package com.hht.honghemould;

import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.hht.baselibrary.ARouterUtils;
import com.hht.baselibrary.BaseActivity;
import com.hht.baselibrary.BaseContance;

import java.lang.ref.WeakReference;

public class MainActivity extends BaseActivity {
    private TextView mTextView;
    private int mCountNumber = 5;
    private AppHandler mAppHandler = new AppHandler(this);
    private static final int MSG = 100;
    private static final int TIME = 1000;


    @Override
    public int getLayoutResID() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        mTextView = findViewById(R.id.text_view);
        mAppHandler.sendEmptyMessageDelayed(MSG, TIME);
    }

    private void startActivity() {
//        Intent intent = new Intent(this, MainMouldActivity.class);
//        startActivity(intent);
//        ARouter.getInstance()
//                .build(BaseContance.MAIN_ACTIVITY)
//                .navigation();

          ARouterUtils.getActivity(BaseContance.MAIN_ACTIVITY);

        finish();
    }

    private class AppHandler extends Handler {
        private final WeakReference<MainActivity> mMainActivityWeakReference;

        private AppHandler(MainActivity activity) {
            mMainActivityWeakReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            MainActivity activity = mMainActivityWeakReference.get();
            if (activity == null || activity.isFinishing()) {
                removeCallbacksAndMessages(null);
                return;
            }
            mTextView.setText("倒计时：" + mCountNumber);
            mCountNumber--;
            mAppHandler.sendEmptyMessageDelayed(MSG, TIME);
            if (mCountNumber == 0) {
                startActivity();
            }
        }
    }
}